package com.caocaog.market.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.caocaog.market.entity.MemberAddress;
import com.caocaog.market.mapper.MemberAddressMapper;
import com.caocaog.market.service.IMemberAddressService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户地址管理表
 *
 * @author jeecg-boot
 * @date 2021-12-13
 */
@Service
@Transactional
@RequiredArgsConstructor
public class MemberAddressServiceImpl extends ServiceImpl<MemberAddressMapper, MemberAddress> implements IMemberAddressService {

    @Override
    public List<MemberAddress> getUserAddresses(String memberId) {
        List<String> ascStr = new ArrayList<>();
        ascStr.add("addr_type");
        ascStr.add("addr_label");
        List<MemberAddress> memberAddressList = super.baseMapper.selectList(new QueryWrapper<MemberAddress>()
                .eq("member_id", memberId)
                .orderByAsc(ascStr).orderByDesc("create_time"));
        return memberAddressList;
    }

    @Override
    public MemberAddress getMemberCommonAddress(String memberId) {
        List<String> ascStr = new ArrayList<>();
        ascStr.add("addr_type");
        ascStr.add("addr_label");
        MemberAddress memberAddress = super.baseMapper.selectOne(new QueryWrapper<MemberAddress>()
                .eq("member_id", memberId)
                .orderByAsc(ascStr).orderByDesc("create_time")
                .last(" limit 1 "));
        return memberAddress;
    }
}
